﻿/***************************
* 1.语音导览
****************************/

//变量
var audio = "audio/audio.html";
var audioDetail = "audio/audioDetail.html";

var contactsCallback_audio = myApp.onPageInit('audio', function (page) {
    // 过滤 全部景区
    $$(".navbar-inner .right img").hide();

    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    // JSON
    var conditionParams = JSON.stringify({
        AccountGuid: account
    });

    //景区列表
    $$.ajax({
        url: ISS_Constants.Url.const_wechat_Attraction,
        data: { "conditionParm": conditionParams },
        crossDomain: true,
        method: "POST",
        dataType: "jsonp",
        success: function (result, status) {
            result = JSON.parse(result);
            if (result && result.datas && result.datas.length > 0) {
                var searchTemplate = $$('#template_Audio_Attraction').html();
                if (searchTemplate) {
                    var compiledTemplate = Template7.compile(searchTemplate);
                    var html = compiledTemplate(result);
                    $$("#mywrapper").append(html);
                }
            }
            //滚动条
            //            var mySwiper3 = myApp.swiper('.swiper-3', {
            //                pagination: '.swiper-3 .swiper-pagination',
            //                spaceBetween: 0,
            //                slidesPerView: 3
            //            });

            var mySwiper = new Swiper('.swiper-container', {
				pagination: '.pagination',
				paginationClickable: true,
                slidesPerView: 'auto',
                centeredSlides: true,
				mode : 'horizontal',
				onSlideChangeStart: function() {
					$$(".tabs .active").removeClass('active');
					$$(".tabs a").eq(mySwiper.activeIndex == null ? 0 : mySwiper.activeIndex).addClass('active');
				},
				onFirstInit : function(){
				},
            });
            $$(".tabs a").eq(0).addClass('active');
        },
        error: function (xhr, status) { }
    });
})

var contactsCallback_audioDetail = myApp.onPageInit('audioDetail', function (page) {

    //获取页面参数
    var AttractionId = ISS.utils.getParameter("scode");
    if (page != undefined) {
        AttractionId = page.query.scode;
    }
    // JSON
    var conditionParams = JSON.stringify({
        AttractionId: AttractionId
    });

    //景区列表
    $$.ajax({
        url: ISS_Constants.Url.const_wechat_Scenic,
        data: { "conditionParm": conditionParams },
        crossDomain: true,
        method: "POST",
        dataType: "jsonp",
        success: function (result, status) {
            result = JSON.parse(result);
            if (result && result.datas && result.datas.length > 0) {
                var searchTemplate = $$('#template_Audio_Scenic').html();
                if (searchTemplate) {
                    var compiledTemplate = Template7.compile(searchTemplate);
                    var html = compiledTemplate(result);
                    $$("#mywrapper").append(html);
                }
            }
            //滚动条
            var mySwiper = new Swiper('.swiper-container', {
				pagination: '.pagination',
				paginationClickable: true,
                slidesPerView: 'auto',
                centeredSlides: true,
				mode : 'horizontal',
				onSlideChangeStart: function() {
					$$(".tabs .active").removeClass('active');
					$$(".tabs a").eq(mySwiper.activeIndex == null ? 0 : mySwiper.activeIndex).addClass('active');
				},
				onFirstInit : function(){
				},
            });
             //默认选中第一个
             $$(".tabs a").eq(0).addClass('active');
        },
        error: function (xhr, status) { }
    });

    //播放器
    var media = $$("#audio-control")[0];

    //点击播放事件
    $$("#audio_page").on("click", ".play-but span", function (e) {
        //播放
        playAudio(this);
        e.stopPropagation();
    });

    //播放暂停切换  
    function playAudio(span) {
        //如果当前暂停
        if (media.paused) {
            if($$(media).attr("scenicId") == undefined || $$(media).attr("scenicId") == null 
               || $$(media).attr("scenicId") != span.id){
                   //scenicId
                   $$(media).attr("scenicId",span.id);
                   //语音连接
                   media.src = span.parentElement.attributes["audio"].nodeValue;
               }
            //修改其他图片
            $$("#audio_page .play-but span").css("background","rgba(0, 0, 0, 0) url('http://localhost:666/Common/Images/audio/Play.png') no-repeat scroll 50% 50% / 25px 23px padding-box border-box");
            //背景图
            $$(span).css("background","rgba(0, 0, 0, 0) url('http://localhost:666/Common/Images/audio/Play.gif') no-repeat scroll 50% 50% / 25px 23px padding-box border-box");
            //播放
            play(media);
        } 
        else {
            if($$(media).attr("scenicId") != span.id){
                   //scenicId
                   $$(media).attr("scenicId",span.id);
                   //语音连接
                   media.src = span.parentElement.attributes["audio"].nodeValue;
                   //修改其他图片
                   $$("#audio_page .play-but span").css("background","rgba(0, 0, 0, 0) url('http://localhost:666/Common/Images/audio/Play.png') no-repeat scroll 50% 50% / 25px 23px padding-box border-box");
                   //背景图
                   $$(span).css("background","rgba(0, 0, 0, 0) url('http://localhost:666/Common/Images/audio/Play.gif') no-repeat scroll 50% 50% / 25px 23px padding-box border-box");
                   //播放
                   play(media);
               }
            else if($$(media).attr("scenicId") == span.id){
                   //修改其他图片
                   $$("#audio_page .play-but span").css("background","rgba(0, 0, 0, 0) url('http://localhost:666/Common/Images/audio/Play.png') no-repeat scroll 50% 50% / 25px 23px padding-box border-box");
                   //暂停
                   pause(media)
               }
            
        }
    }

    //播放  
    function play(media) {
        media.play();
        $$('#play').html('Pause');
    }

    //暂停  
    function pause(media) {
        media.pause();
        $$('#play').html('Play');
    }

    // 过滤 全部景区
    $$(".navbar-inner .right img").hide();
})

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(audio) > -1) {
        contactsCallback_audio.trigger();
    }
    else if (href.indexOf(audioDetail) > -1) {
        contactsCallback_audioDetail.trigger();
    }
}

initData();
